﻿package frEngine.loaders.away3dMd5
{
    import __AS3__.vec.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.loaders.away3dMd5.md5Data.*;
    import frEngine.math.*;

    public class MD5AnimParserBase extends Object
    {
        public var name:String;
        protected var _curClip:SkeletonClipNode;
        public var paserCompleted:Boolean;
        public var hierarchy:Vector.<HierarchyData>;
        public var baseFrameData:Vector.<BaseFrameData>;
        protected var _numJoints:uint;
        protected var _bytes:ByteArray;
        protected var _curFrame:int;
        protected var _numAnimatedComponents:uint;
        private var preFrameIndex:int = -1;
        private var pre:SkeletonPose;
        public var infoName:String;

        public function MD5AnimParserBase(param1:String)
        {
            this.infoName = param1;
            return;
        }// end function

        public function clear() : void
        {
            this._numAnimatedComponents = 0;
            this._numJoints = 0;
            this.hierarchy = null;
            this.baseFrameData = null;
            this._curClip = null;
            this._bytes = null;
            this.paserCompleted = false;
            return;
        }// end function

        public function proceedParsing() : Boolean
        {
            var _loc_1:* = this.readClipFrames(this._curClip);
            if (_loc_1)
            {
                this._curClip.setLoaded();
                this._curClip = null;
            }
            return _loc_1;
        }// end function

        public function parseVector3D() : Vector3D
        {
            var _loc_1:* = new Vector3D();
            _loc_1.x = this._bytes.readFloat();
            _loc_1.y = this._bytes.readFloat();
            _loc_1.z = this._bytes.readFloat();
            return _loc_1;
        }// end function

        protected function setClip(param1:SkeletonClipNode) : void
        {
            this._curClip = param1;
            var _loc_2:* = this._bytes.readUnsignedShort();
            var _loc_3:* = this.parseVector3D();
            var _loc_4:* = this.parseVector3D();
            var _loc_5:* = this.parseVector3D();
            this._curFrame = 0;
            return;
        }// end function

        protected function readClipFrames(param1:SkeletonClipNode) : Boolean
        {
            var _loc_2:FrameData = null;
            var _loc_6:uint = 0;
            var _loc_7:int = 0;
            var _loc_8:SkeletonPose = null;
            var _loc_3:* = param1.numFrames;
            var _loc_4:* = TimeControler.minFpsTime;
            var _loc_5:* = TimeControler.stageTime;
            if (_loc_3 > 1000)
            {
                throw new Error("动作" + param1.trackName + "总帧数:" + param1.trackName);
            }
            if (getTimer() - _loc_5 > _loc_4)
            {
                return false;
            }
            while (this._curFrame < _loc_3)
            {
                
                if (getTimer() - _loc_5 > _loc_4)
                {
                    break;
                }
                _loc_2 = new FrameData();
                _loc_2.components = new Vector.<Number>(this._numAnimatedComponents, true);
                _loc_6 = this._bytes.readUnsignedShort();
                _loc_7 = 0;
                while (_loc_7 < this._numAnimatedComponents)
                {
                    
                    _loc_2.components[_loc_7] = this._bytes.readFloat();
                    _loc_7++;
                }
                _loc_8 = new SkeletonPose(this, _loc_2, this._numJoints);
                param1.addFrame(_loc_8);
                this.pre = _loc_8;
                this.preFrameIndex = _loc_6;
                var _loc_9:String = this;
                var _loc_10:* = this._curFrame + 1;
                _loc_9._curFrame = _loc_10;
            }
            return this._curFrame >= _loc_3;
        }// end function

        private static function initRotationQuat(param1:Vector3D = null, param2:Number = 0) : Quaternion
        {
            var _loc_3:* = new Quaternion();
            var _loc_4:* = new Quaternion();
            var _loc_5:* = new Quaternion();
            _loc_4.fromAxisAngle(Vector3D.X_AXIS, (-Math.PI) * 0.5);
            _loc_5.fromAxisAngle(Vector3D.Y_AXIS, -Math.PI);
            _loc_3.multiply(_loc_5, _loc_4);
            if (param1)
            {
                _loc_4.fromAxisAngle(param1, param2);
                _loc_3.multiply(_loc_4, _loc_3);
            }
            return _loc_3;
        }// end function

        public static function checkIsCompressBone(param1:uint, param2:uint) : Boolean
        {
            var _loc_3:Boolean = false;
            if (param2 >= 256 && param2 < 260)
            {
                param2 = param2 - 256;
            }
            if (param1 == MD5AnimByteArrayParser.parseTypeValue)
            {
                if (param2 >= 1 && param2 <= 3)
                {
                    _loc_3 = true;
                }
                else
                {
                    _loc_3 = false;
                }
            }
            else
            {
                _loc_3 = false;
            }
            return !_loc_3;
        }// end function

    }
}
